<?php
namespace Addons\Wukong\Model;

use Think\Model;

/**
 * Class WukongMessageModel   阿里悟空消息模型
 * @package Addons\Wukong\Model
 * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
 */
class WukongMessageModel extends WukongModel
{

    /**
     * sendText   发送文字消息
     * @param $conversation_id
     * @param $sender
     * @param $content
     * @param string $receivers
     * @param string $msgType
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    public function sendText($conversation_id, $sender, $content, $receivers = '', $msgType = 'SELF')
    {
        $this->sendMessage($conversation_id, $sender, array('contentType' => 'TEXT', 'text' => $content), $receivers, $msgType);
    }

    /**
     * sendMessage  发送消息
     * @param $conversation_id
     * @param $sender
     * @param $content
     * @param string $receivers
     * @param string $msgType
     * @return array
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    public function sendMessage($conversation_id, $sender, $content, $receivers = '', $msgType = 'SELF')
    {

        $sender = $sender == null ? is_login() : $sender;
        $url = $this->_host . '/v1/im/message/send';

        $array = array(
            'senderId' => $sender,
            'content' => $content,
            'msgType' => $msgType,
            'conversationId' => $conversation_id,
            "tag" => 100,
            "extension" => array(
                "key1" => "value1",
                "key2" => "value2",
                "key3" => "value3"
            )
        );
        if ($receivers) {
            $array['receivers'] = $receivers;
        }
        $json = json_encode($array);
        $res = $this->visitUrl($url, 'POST', $json);
        return $res;

    }


    /**
     * getMessageList   获取会话消息列表
     * @param $conversation_id
     * @param $openId
     * @return array
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    public function getMessageList($conversation_id, $openId=null, $forward = true, $cursor = 0, $count = 20)
    {
        $openId = $openId == null ? is_login() : $openId;
        $url = $this->_host . '/v1/im/message/list ';
        $array = array(
            'openId' => $openId,
            'conversationId' => $conversation_id,
            'forward' => $forward,
            'cursor' => $cursor,
            'count' => $count

        );
        $params = http_build_query($array);
        $res = $this->visitUrl($url . '?' . $params, 'GET');
        return $res;

    }


    public function getMessage($message_id,$openId=null){

        $openId = $openId == null ? is_login() : $openId;
        $url = $this->_host . '/v1/im/message';
        $array = array(
            'openId' => $openId,
            'messageId' => $message_id,

        );
        $params = http_build_query($array);
        $res = $this->visitUrl($url . '?' . $params, 'GET');
        return $res;
    }


    public function setRead($ids,$openId=null){

        $openId = $openId == null ? is_login() : $openId;

        $url = $this->_host . '/v1/im/message/set/read';
        $array = array(
            'openId' => $openId,
            'messageIds' => $ids,
        );

        $json = json_encode($array);
        $res = $this->visitUrl($url, 'POST', $json);
        return $res;
    }


}